define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/index/index',
                    // add_url: 'user/user/add',
                    // edit_url: 'user/user/edit',
                    // del_url: 'user/user/del',
                    // multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);


            // 获取选中项
            $(document).on("click", ".btn-selected", function () {
                var data = JSON.stringify(Table.api.selectedids(table));
                var id = Config.id;
                $.ajax({
                    type : "POST",
                    url : "user/index/coupons",
                    data : {"data":Table.api.selectedids(table),'id':id},
                    // contentType : "application/json",
                    dataType : "Json",
                    success: function (result) {
                        Layer.alert(result.msg);
                        setTimeout(function() {
                            window.history.go(-1);
                        },3000);
                    }
                });
                // Layer.alert(JSON.stringify(Table.api.selectedids(table)));
            });

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法

                custom: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    return '<a class="btn-change text-success" data-url="user/user/change" data-confirm="确定审核？" data-id="' + row.id + '"><i class="fa ' + (row.status == '' ? 'fa-toggle-on fa-flip-horizontal text-gray' : 'fa-toggle-on') + ' fa-2x"></i></a>';
                },
            },
        }
    };
    return Controller;
});